import { getData } from '@vimi/utils-api'
import { SERVICE_NAME } from '@/gov/plugins/constant'
const state = {
  importTemplate: [],
}

const getters = {
  importTemplate: state => state.importTemplate,
}

const actions = {
  async getImportTemplate({ commit, rootGetters }, force = true) {
    if (!force && state.importTemplate.length) {
      return Promise.resolve(state.importTemplate)
    }
    const defaultImportKey = 'datum_one_county_industry_template_bank'
    const res = await getData(SERVICE_NAME, 'import_template', {
      'template_key.endlike': defaultImportKey,
      'agency_type.eq': rootGetters.user.clientOrgClassType,
    })
    const data = (res && res.data) || []
    commit('importTemplate', data)
    return data
  },
}

const mutations = {
  importTemplate: (state, payload) => (state.importTemplate = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
